﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class DeliveryNoteReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b;
        string MultiRowTable = "WASTGA;WASTPASS;BASTGA;GASTGA;WTLEIST;BASTKE;";
        int WarpBeamEnds11 = 0;
        int TotalEnds = 0;
        int TotalSelvage = 0;
        string WBBAUFKRS;
        string WBBAUFLFD;
        string WBBAUFPOS;
        string WBBALIEFSCH;
        string WBBTAX;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        double Warpdensity = 1.0;
        double Weftdensity = 1.0;
        double Totalends = 0;
        double WeftEnds = 1.0;
        double Jdense = 0.0;
        double Wdense = 0.0;
        double TotalDense = 0.0;
        #endregion
        public DeliveryNoteReport()
            : base()
        {
        }
        public DeliveryNoteReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            WBBAUFKRS = dicParamater["WBBAUFKRS"].ToString().Trim();
            WBBAUFLFD = dicParamater["WBBAUFLFD"].ToString().Trim();
            WBBAUFPOS = dicParamater["WBBAUFPOS"].ToString().Trim();
            WBBALIEFSCH = dicParamater["WBBALIEFSCH"].ToString().Trim();
            WBBTAX = dicParamater["WBBTAX"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        #region------------------------------Function------------------------


        private string Byte2String(byte[] arrByte)
        {

            StringBuilder sb = new StringBuilder();

            foreach (byte b in arrByte)
            {

                sb.Append(b > 15 ? Convert.ToString(b, 16) : '0' + Convert.ToString(b, 16));

            }

            return sb.ToString();

        }
     
        private void HideNullRow()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);
                var val2 = exAgent.GetCellValue(1, 1);
                var val3 = exAgent.GetCellValue(1, 1);
                //Case 1
                rowstart = 30; rowend = 51; col1 = 3;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 9, 38);
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col1);
                    }
                }
                rowstart = 53; rowend = 56; col1 = 2; col2 = 3;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 9, 38);
                    if ((string)val1 == "0")

                        //.get_Value(Type.Missing);
                        if ((string)val1 == "0")
                        {
                            HidedRows++;
                            exAgent.HideRows(Rowi, col1, Rowi, col1);
                        }
                }
                rowstart = 341; rowend = 351; col1 = 17; col2 = 23;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 3, 4);
                    val2 = exAgent.GetRangeValue(Rowi, Rowi, 34, 35);
                    if ((string)val1 == "0" && (string)val2 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
                /*
                             rowstart = 381 + HidedRows - 21; rowend = 387 + HidedRows - 21; col1 = 3; col2 = 22;
                             for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                             {
                                 val1 = exAgent.GetCellValue(Rowi, col1);
                                 val2 = exAgent.GetCellValue(Rowi, col2);
                                 if (val1 == null && val2 == null)
                                 {
                                     HidedRows++;
                                     exAgent.HideRows(Rowi, col1, Rowi, col2);
                                 }
                             }
                             */
                int InsertedRows = HidedRows - 18;
                if (HidedRows > 0)
                {
                    rowstart = 359;
                    exAgent.InsertRows(rowstart, HidedRows - 18);
                }
                rowstart = 433 + InsertedRows; rowend = 1800 + InsertedRows;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 8, 34);
                    if ((string)val1 == "0")
                    {
                        if (Rowi == rowstart)
                            Rowi = Rowi - 1;
                        exAgent.HideRows(Rowi, col1, rowend, col1);
                        Rowi = rowend;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FillInVKPOS(DataTable dtSource)
        {

           try
            {
            
            Queue CellQueue = new Queue();
            CellQueue.Clear();

            foreach (DataRow dwSource in dtSource.Rows)
            {
                CellName = "SAL_ORDER";
                CellValue = dwSource["VKPAUFKRS"].ToString().Trim() + "/" + dwSource["VKPAUFLFD"].ToString().Trim();
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "CUSTOMER";
                CellValue = dwSource["KDNAME1"].ToString().Trim();
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "PO";
                CellValue = dwSource["VKPKDKOMM"].ToString().Trim();
                exAgent.SetCellValue(CellName, CellValue, true); 
                CellName = "SAL_ARTICLE_DES";
                CellValue = dwSource["VKPKDBEZ"].ToString().Trim() + dwSource["VKPKDARTNR"].ToString().Trim() + dwSource["VKPKDFARBNR"].ToString().Trim();
                exAgent.SetCellValue(CellName, CellValue, true);

            }
           }
          catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInVVTLIEF(DataTable dtSource)
        {

            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();

                foreach (DataRow dwSource in dtSource.Rows)
                {
                    CellName = "deliverynote";
                    CellValue = dwSource["VVTLIEFSCH"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInCOLOR(DataTable dtSource)
        {

            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();

                foreach (DataRow dwSource in dtSource.Rows)
                {

                    CellName = "SAL_COLOR";
                    CellValue = dwSource["DES"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true); 
                    
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FillInWBBST(DataTable dtSource)
        {
            try
            {
                Queue CellQueue = new Queue();
                CellQueue.Clear();
                
                string YarnNum = "000";
                int Yarni = 0;

                
                
                foreach (DataRow dwSource in dtSource.Rows)
                {

                    CellName = "SAL_COLOR";
                    CellValue = dwSource["DES"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true); 
                            Yarni++;
                            //GAART,   GAVAR,GAFARBE,GAKG1,GAKG3,ZUPARTIE,GASPUL, GPSPPART,LSNAME1
                            YarnNum = Right("000" + Yarni.ToString(), 3);
                            string ACTYAND = dwSource["ACTYAND"].ToString();
                            string CODE = dwSource["WBBBEM"].ToString();

                            CellName = "PIECE" + YarnNum;
                            CellValue = ACTYAND;
                            exAgent.SetCellValue(CellName, CellValue, true);
                            CellName = "CODE" + YarnNum;
                            CellValue = CODE;
                            exAgent.SetCellValue(CellName, CellValue, true);
                            
                   
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public void GetDeliveryNoteReport(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "DeliveryNote";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
                    filePath =   strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = Path.Combine(PathHelper.ApplicationPath, @"Report\"  + WBBAUFKRS +"_"+ WBBAUFLFD + ".xls");
                        sheetName = WBBAUFKRS + "_" + WBBAUFLFD + "_" + WBBALIEFSCH + "_" + WBBTAX;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        CellName = "PrintDate";
                        value = System.DateTime.Today.ToShortDateString();
                        exAgent.SetCellValue(CellName, value, true);
                               CellName = "PageNum";
                        value = "1";
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "YEAR";
                        value = System.DateTime.Today.Year.ToString();
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "MONTH";
                        value = System.DateTime.Today.Month.ToString();
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "DAY";
                        value = System.DateTime.Today.Day.ToString();
                        exAgent.SetCellValue(CellName, value, true);
                        FillInVKPOS(dsSource.Tables["VKPOS"]);
                        FillInVVTLIEF(dsSource.Tables["VVTLIEF"]);
                        //FillInCOLOR(dsSource.Tables["COLOR"]);
                        FillInWBBST(dsSource.Tables["WBBST"]);
                        //FillInWBBTAX(dsSource.Tables["WBBTAX"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (strCompany == "HLT")
                {
                    //HideNullRow();
                }
                else
                {
                    //HideNullRow();
                }
                exAgent.Save();
                //MessageBox.Show("你是否打开Exclel?",,MessageBoxButtons.YesNo,MessageBoxDefaultButton.Button2);

                exAgent.Close();
            }
        }


    }

}
